//
//  ItemVC2.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#import "ItemVC2.h"
#import "NetworkHandlerBlock.h"
#import "ItemVC2TableViewCell.h"
#import "ModelForListen.h"
#import "MJRefresh.h"
#import "DataBaseHandler.h"

#import "SecendViewController.h"


@interface ItemVC2 ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *arrForListen;

@end

@implementation ItemVC2

- (void)viewDidLoad {
    [super viewDidLoad];

    [self handleData];
    NSLog(@"%@,%lu",self.arrForListen,self.arrForListen.count);
//    if (self.arrForListen.count) {
        [self createTableView];
//    }else {
//        UIImageView *backGround = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wuwang"]];
//        backGround.frame = self.view.bounds;
//        [self.view addSubview:backGround];
//        [backGround release];
//    }
  
    self.title = @"发现";
//
//         self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//        // 进入刷新状态后会自动调用这个block
//        [self handleData];
//        
//        [self.tableView reloadData];
//        
//        [self.tableView.mj_header endRefreshing];
//    }];

   
}

- (void)handleData {

    self.arrForListen = [NSMutableArray array];

    [NetworkHandlerBlock netWorkingWithURL:@"http://mobile.ximalaya.com/mobile/album/recommend/list/unlogin?device=android" completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        NSDictionary *tempDic = [NSDictionary dictionaryWithDictionary:result];
        NSArray *tempArr = [NSArray arrayWithArray:[tempDic objectForKey:@"frDatas"]];
        for (NSDictionary *dic in tempArr) {
            ModelForListen *model = [[ModelForListen alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForListen addObject:model];
        }
          [self.tableView reloadData];
    }];
}

#pragma mark createTableView
- (void)createTableView {

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[ItemVC2TableViewCell class] forCellReuseIdentifier:@"poolForUITableViewCell"];
    
}

#pragma mark tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrForListen.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForListen *model = [self.arrForListen objectAtIndex:indexPath.row];
    ItemVC2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForUITableViewCell"];
    [cell passModelForListen:model];
    return cell;
}

#pragma mark tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForListen *model = [self.arrForListen objectAtIndex:indexPath.row];
    SecendViewController *secend = [[SecendViewController alloc]init];
    secend.stringForAlbumID = model.albumId;
    secend.modelForListen = model;

    [self.navigationController pushViewController:secend animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
